package com.ruoyi.controller.system;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.vo.AjaxResult;
import com.ruoyi.controller.BaseController;
import com.ruoyi.domain.SysDictType;
import com.ruoyi.service.SysDictTypeService;

@RestController
@RequestMapping("system/dict/type")
public class SysDictTypeController extends BaseController{
	
	@Autowired
	private SysDictTypeService sysDictTypeService;
	
	
	/**
	 * 获取字典类型列表
	 * @return
	 */
	@GetMapping("list")
	public TableDataInfo list() {
		logger.info("获取字典类型列表");
		startPage();
		List<SysDictType> list = sysDictTypeService.selectDictTypeList();
		return getDataTable(list);
	}
	
	/**
	 * 根据字典id查询字典类型详细
	 * @param dictId
	 * @return
	 */
	@GetMapping(value = "{dictId}")
	public AjaxResult getInfo(@PathVariable Long dictId) {
		logger.info("根据字典id查询字典类型详细,dictId:{}",dictId);
		return AjaxResult.success(sysDictTypeService.selectDictTypeById(dictId));
	}
	
	/**
	 * 新增字典类型
	 * @param dictType
	 * @return
	 */
	@PostMapping
	public AjaxResult add(@Validated @RequestBody SysDictType dict) {
		logger.info("新增字典类型,dictType:{}",dict);
		if (!sysDictTypeService.checkDictTypeUnique(dict)) {
			return error("新增字典类型" + dict.getDictType() + "失败，字典类型已存在");
		}
		dict.setCreateBy(getUsername());
		return toAjax(sysDictTypeService.insertDictType(dict));
	}
	
	/**
	 * 修改字典类型
	 * @param dict
	 * @return
	 */
	@PutMapping
	public AjaxResult edit(@Validated @RequestBody SysDictType dict) {
		logger.info("修改字典类型,dict:{}",dict);
		if (!sysDictTypeService.checkDictTypeUnique(dict)) {
			return error("修改字典类型" + dict.getDictType() + "失败，字典类型已存在");
		}
		dict.setUpdateBy(getUsername());
		return toAjax(sysDictTypeService.updateDictType(dict));
	}
	
	/**
	 * 删除字典类型
	 * @param dictId
	 * @return
	 */
	@DeleteMapping(value = "/{dictId}")
	public AjaxResult remove(@PathVariable Long dictId) {
		logger.info("删除字典类型,dictIdType:{}",dictId);
		sysDictTypeService.deleteDictTypeById(dictId);
		return AjaxResult.success();
	}
	
	/**
	 * 获取字典选择框列表
	 * @return
	 */
	@GetMapping("optionselect")
	public AjaxResult optionselect() {
		List<SysDictType> dictType = sysDictTypeService.selectDictTypeAll();
		return AjaxResult.success(dictType);
	}
}
